package com.android.crypto_benchmark;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.util.Date;

import javax.crypto.Cipher;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class Crypto_Benchmark extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		long result = 0;
		long somma = 0;
		long num_test = 10;
		int plaintextlenght = 117;
		Log.v("[BENCHMARK]", "entro qua dentro?1");
		try {
			 Log.v("[BENCHMARK]", "entro qua dentro?");
			 for (int i=0; i<num_test; i++){
			 somma +=  benchCryptOfAMsg(generateRandomPlainText(plaintextlenght));
			 Log.v("[BENCHMARK]", "" + somma);
			 }
			//Byte [] list=generateBlock(generateRandomPlainText(plaintextlenght));
			//result = benchCryptOfAMsg(generateRandomPlainText(plaintextlenght));

		} catch (Exception e) {
			
			e.printStackTrace();
		}
		result = somma/num_test;
		Date d = new Date();
		TextView tv = new TextView(this);

		tv.setText("Somma: " + new Long(somma).toString() + "\nResult: "
				+ new Double(result).toString() + "ms");

		setContentView(tv);
	}

	private Byte[] generateBlock(String generateRandomPlainText) {
		return null;
	}

	private String generateRandomPlainText(int plaintextlenght) {
		String retstring="";
		for (int i = 0; i <plaintextlenght; i++){
			double randomNumberSetup = (Math.random() * 26 + 'a');
            retstring += (char) randomNumberSetup;
		}
		return retstring;
	}

	private long benchCryptOfAMsg(String string) throws Exception {
		byte[] plainText = string.getBytes("UTF8");
		//
		// Generate an RSA key
		Log.v("[BENCHMARK]", "Start crypting");
		//output = "\nStart generating RSA key";
		KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
		keyGen.initialize(1024);
		KeyPair key = keyGen.generateKeyPair();
		//output += "Finish generating RSA key: " + key.toString();
		//
		// Creates an RSA Cipher object (specifying the algorithm, mode, and
		// padding).
		Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
		//
		// Print the provider information
		//output += "\n" + cipher.getProvider().getInfo();
		//output += "\nStart encryption";
		//
		// Initializes the Cipher object.
		long start= new Date().getTime();
		cipher.init(Cipher.ENCRYPT_MODE, key.getPublic());
		//
		// Encrypt the plaintext using the public key
		
		byte[] cipherText = cipher.doFinal(plainText);
		long stop=new Date().getTime();
		//output += "Finish encryption: ";
		//output += new String(cipherText, "UTF8");
		//output += "\nStart decryption";
		//
		// Initializes the Cipher object.
		cipher.init(Cipher.DECRYPT_MODE, key.getPrivate());
		//
		// Decrypt the ciphertext using the private key
		byte[] newPlainText = cipher.doFinal(cipherText);
		Log.v("[BENCHMARK]", new String(newPlainText, "UTF8"));
		//output += "Finish decryption: ";
		//output += new String(newPlainText, "UTF8");
		
		return (stop - start);
	}
}
